package demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import util.Configuration;
import util.po.ConfigPO;

/**
 * Socket客户端
 * 
 * @author sundy.hu
 * 
 * @version 1.0
 * 
 * @date 2012-11-28
 * 
 */
public class Client implements Runnable {

	// Socket 服务端地址
	private String address;

	// Socket服务端端口
	private int port;

	// 要发送的文件夹
	private File file;

	private Logger log = Logger.getLogger(Client.class.getName());

	private ConfigPO config = new Configuration().configure();

	// 设置要发送的文件夹
	public void setFile(File file) {
		this.file = file;
	}

	public Client() {
		this.address = config.getServer2SocketAddress();
		this.port = config.getServer2SocketPort();
		this.log.setLevel(Level.INFO);
		this.file = new File(config.getSendPath());
	}

	@Override
	public void run() {
		try {
			Socket client = new Socket(address, port);
			send(client);
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}

	// 发送文件
	private void send(Socket client) {
		if (null != client) {
			try {
				String currentFileName = System.currentTimeMillis()
						+ config.getReceiveExtName();
				if (file.isFile()) {
					BufferedInputStream input = new BufferedInputStream(
							new FileInputStream(file));
					DataInputStream dis = new DataInputStream(input);
					BufferedOutputStream output = new BufferedOutputStream(
							client.getOutputStream());
					DataOutputStream dos = new DataOutputStream(output);
					//byte[] b = new byte[1024];
					int len = 0;
					client.setReceiveBufferSize(10 * config.getSendBufferSize());
					while ((len = dis.read()) != -1) {
						dos.write((char)len);
						if (len % client.getSendBufferSize() == 0) {
							dos.flush();
						}
					}
					dos.close() ;
					log.info("send file....");
					input.close();
					output.flush();
					output.close();
					client.close();
				} else if (file.isDirectory()) {
					// 将要发送的文件打包

					ZipOutputStream zos = new ZipOutputStream(
							new BufferedOutputStream(new FileOutputStream(
									currentFileName)));
					for (File f : file.listFiles()) {
						if (!f.canRead()) {
							continue;
						}
						ZipEntry entry = new ZipEntry(f.getName());
						entry.setComment(String.valueOf(System
								.currentTimeMillis()));
						InputStream input = new FileInputStream(f);
						zos.putNextEntry(entry);
						log.info("getter file info ");
						int len = 0;
						
						while ((len = input.read()) != -1) {
							zos.write((char) len);
							if (len % config.getSendBufferSize() == 0) {
								zos.flush();
							}

						}
						input.close();
					}
					zos.flush();
					zos.close();

					BufferedInputStream input = new BufferedInputStream(
							new FileInputStream(currentFileName));
					DataInputStream dis = new DataInputStream(input);
					BufferedOutputStream output = new BufferedOutputStream(
							client.getOutputStream());
					DataOutputStream dos = new DataOutputStream(output);
					//ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
					int len = 0;
					client.setReceiveBufferSize(12 * config.getSendBufferSize());
					// byte[] b = new byte[1024] ;
					while ((len = dis.read()) != -1) {
						dos.write((char) len);
						//baos.write((char)len) ;
						if (len % client.getSendBufferSize() == 0) {
							dos.flush();
						}
						// output.write((char) len);
					}
					log.info("send file....");
				//	byte[] b = baos.toByteArray() ;
					//baos.close() ;
					//System.out.println(b.length);
					input.close();
					output.flush();
					output.close();
					client.close();
					new File(currentFileName).delete();
				}

				System.out.println("files send successful");
				/*
				 * InputStream input = new FileInputStream(fileName); int len =
				 * 0; OutputStream output = client.getOutputStream(); while
				 * ((len = input.read()) != -1) { output.write((char) len); }
				 * input.close(); output.flush(); output.close();
				 * System.out.println(""); client.close();
				 */
			} catch (IOException ex) {
				log.warning(ex.getLocalizedMessage());
				ex.printStackTrace();
			}
		}
	}
}
